<?php
/**
 * Created by PhpStorm.
 * User: 任旭明 <846723340@qq.com>
 * Date: 2018/9/27
 * Time: 18:09
 */

namespace app\index\controller;

use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
use think\Controller;
use think\Request;

class UpLoadImg extends Controller
{
	static private $accessKey = '3roDf6g24Fk8hRh65dWNBR_7zaz-0BczaMVsKzBs';
	static private $secretKey = 'VKL8w95zTHL-9YC0_a70DhiA9Wik8fR278d7-tZl';
	static private $bucketName = 'picture';

	public function index($file, $img_name, $path)
	{
		$filePath = $file->getRealPath();
		$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
		$key = $path . '_' . $img_name . $ext;
		$upManager = new UploadManager();
		$auth = new Auth(self::$accessKey, self::$secretKey);
		$token = $auth->uploadToken(self::$bucketName);
		list($ret, $error) = $upManager->putFile($token, $key, $filePath);
		if ($error !== null) {
			return ["err" => 1, "msg" => $error, "data" => ""];
		} else {
			//返回图片的完整URL
			$imgPath = $key;
			return $imgPath;
		}
	}


}